package vn.bk.hotel.adapter;

import java.util.ArrayList;

import vn.bk.hotel.object.HotelInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseHotel {
    private SQLiteDatabase database;
    private MySQLiteAdapter dbHelper;
    private String[] allColumns = MySQLiteAdapter.columns;

    public DatabaseHotel(Context context) {
	dbHelper = new MySQLiteAdapter(context);
    }

    private void open() throws SQLException {
	database = dbHelper.getWritableDatabase();
    }

    public void close() {
	dbHelper.close();
    }

    public HotelInfo createHotel(HotelInfo hotel) {
	open();
	ContentValues values = new ContentValues();
	values.put(MySQLiteAdapter.COLUMN_NAME, hotel.getNameHotel());
	values.put(MySQLiteAdapter.COLUMN_TYPE, hotel.getType());
	values.put(MySQLiteAdapter.COLUMN_ADDRESS, hotel.getAdress());
	values.put(MySQLiteAdapter.COLUMN_NUMRATE, hotel.getNumRate());
	values.put(MySQLiteAdapter.COLUMN_URL_BITMAP, hotel.getUrlBitMapHotel());
	values.put(MySQLiteAdapter.COLUMN_LONG, hotel.getLongitude());
	values.put(MySQLiteAdapter.COLUMN_LAT, hotel.getLatitude());
	values.put(MySQLiteAdapter.COLUMN_PHONE, hotel.getPhone());
	values.put(MySQLiteAdapter.COLUMN_URI, hotel.getUri());
	values.put(MySQLiteAdapter.COLUMN_DES, hotel.getDescription());

	long insertId = database.insert(MySQLiteAdapter.TABLE_NAME, null,
		values);
	Cursor cursor = database.query(MySQLiteAdapter.TABLE_NAME, allColumns,
		MySQLiteAdapter.COLUMN_ID + " = " + insertId, null, null, null,
		null);
	cursor.moveToFirst();
	HotelInfo newHotel = cursorToHotel(cursor);
	cursor.close();
	close();
	return newHotel;
    }

    public void deleteHotel(HotelInfo hotel) {
	open();
//	database.delete(MySQLiteAdapter.TABLE_NAME, MySQLiteAdapter.COLUMN_URI
//		+ " = " + hotel.getUri(), null);
	database.execSQL("delete from " + MySQLiteAdapter.TABLE_NAME
		+ " where " + MySQLiteAdapter.COLUMN_URI + " = '"
		+ hotel.getUri()+"'");
	close();
    }

    public ArrayList<HotelInfo> getAllHotels() {

	open();
	ArrayList<HotelInfo> allHotel = new ArrayList<HotelInfo>();

	Cursor cursor = database.query(MySQLiteAdapter.TABLE_NAME, allColumns,
		null, null, null, null, null);

	cursor.moveToFirst();
	while (!cursor.isAfterLast()) {
	    HotelInfo hotel = cursorToHotel(cursor);
	    allHotel.add(hotel);
	    cursor.moveToNext();
	}
	// Make sure to close the cursor
	cursor.close();
	close();
	return allHotel;
    }

    public boolean isExistInDatabase(HotelInfo myHotel) {
	ArrayList<HotelInfo> allHotel = getAllHotels();
	for (int i = 0; i < allHotel.size(); i++) {
	    if (allHotel.get(i).getUri().equals(myHotel.getUri())) {
		return true;
	    }
	}
	return false;
    }

    private HotelInfo cursorToHotel(Cursor cursor) {

	return new HotelInfo(cursor.getString(1), cursor.getString(2),
		cursor.getString(3), cursor.getInt(4), cursor.getString(5),
		cursor.getDouble(6), cursor.getDouble(7), cursor.getString(8),
		cursor.getString(9), cursor.getString(10), false);
    }
}
